package ui.task;

import cerebrum.cache.RunTimeContextCache;
import javafx.application.Platform;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import ui.panel.showroom.ScreenSaverStage;

import java.time.LocalDateTime;
import java.util.TimerTask;

/**
 * 屏保时钟task
 */
public class ScreenSaverTask extends TimerTask {
    private ScreenSaverStage screenSaver;

    public ScreenSaverTask() {
    }

    public ScreenSaverTask(ScreenSaverStage screenSaver) {
        this.screenSaver = screenSaver;
    }


    @Override
    public void run() {
        if (!RunTimeContextCache.getThreadStatus() || !RunTimeContextCache.getTimerStatus()) {
            cancel();// 停止定时器
        }
        // 更新UI显示
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                Label label = (Label) screenSaver.getRoot().lookup("#" + "spirit_clock");
                label.setText(dataCheck());
                label.setTextFill(new LinearGradient(0, 0, 1, 1.82, true, CycleMethod.REPEAT,
                        new Stop[]{new Stop(0.5f, Color.AQUA), new Stop(0.5f, Color.DARKTURQUOISE)}));
            }

        });
    }

    private String dataCheck() {
        LocalDateTime time = LocalDateTime.now();
        int hour = time.getHour();
        int minute = time.getMinute();
        int second = time.getSecond();
        if (hour >12) {
            hour = hour-12;
        }
        if (hour == 0) {
            hour = 12;
        }
        String hourStr = hour < 10 ? ("0" + hour) : String.valueOf(hour);
        String minuteStr = minute < 10 ? ("0" + minute) : String.valueOf(minute);
        String secondStr = second < 10 ? ("0" + second) : String.valueOf(second);
        return hourStr + "  :  " + minuteStr + "  :  " + secondStr;
    }
}
